package com.sjyp.cms;

import com.jfinal.config.*;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.sjyp.cms.commons.Handler.UrlSuffixHandler;
import com.sjyp.cms.model.*;
import com.sjyp.cms.routes.*;

public class ApplicationConfig extends JFinalConfig{
    /**
     * JFinal常量值 ： 开发模式下， JFinal 会对每次请求输出报告，如输出本次请求的 URL、 Controller、 Method 以及请求所携带的参数
     * @param me
     */
    public void configConstant(Constants me) {
        me.setDevMode(true);//开发模式
        me.setViewType(ViewType.FREE_MARKER);
        //
        me.setError404View("/pages/error/404.html");
        me.setError500View("/pages/error/500.html");
    }

    /**
     * 路由：也就http请求分发器
     * @param me
     */
    public void configRoute(Routes me) {
        me.add(new FrontRoutes());//前台路由
        me.add(new AdminRoutes());//后台路由
    }

    public void configEngine(Engine me) {
    }

    public void configPlugin(Plugins me) {
        //加载配置文件
        this.loadPropertyFile("app.properties");
        //数据源
        DruidPlugin dp = new DruidPlugin(this.getProperty("jdbcUrl"), this.getProperty("userName"), this.getProperty("password"));
        me.add(dp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        me.add(arp);
        arp.setDialect(new MysqlDialect());
        arp.addMapping("t_article", "id", Article.class);
        arp.addMapping("t_column", "id", Column.class);
        arp.addMapping("t_picture", "id", Picture.class);
        arp.addMapping("t_resource", "id", Resource.class);
        arp.addMapping("t_user", "id", User.class);

    }

    /**
     * 全局拦截器，拦截所有Aciton。@Clean加在Controller可以清除。
     * @param me
     */
    public void configInterceptor(Interceptors me) {

    }

     public void configHandler(Handlers me) {
         me.add(new UrlSuffixHandler());
     }
}
